package com.glebusheg.ui
{
	import com.glebusheg.data.AppData;
	import com.glebusheg.data.Item;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Gleb Volkov
	 */
	public class Panel extends Sprite 
	{
		private var _header:Sprite;
		private var _content:Sprite;
		
		private var _scrollArea:ScrollArea;
		private var _scrollBar:UIScrollBar;
		
		private var _itemsData:Vector.<String>;
		
		private var _enabled:Boolean = true;

		public function Panel(header:Sprite, content:Sprite, data:Vector.<String>) 
		{
			mouseEnabled = false;
			
			_itemsData = data;
			
			setupHeader(header);
			setupContent(content);
		}
		
		private function setupHeader(header:Sprite):void 
		{
			_header = header;
			_header.mouseChildren = false;
			_header.buttonMode = true;
			_header.addEventListener(MouseEvent.MOUSE_DOWN, onHeaderMouseDown);
			addChild(_header);
		}
		
		private function setupContent(content:Sprite):void 
		{
			_content = content;
			_content.y = _header.height;
			addChild(_content);
			_content.mouseEnabled = false;
			
			var indent:int = 5;
			
			_scrollArea = new ScrollArea(_content.width - indent * 2, _content.height - indent * 2);
			_scrollArea.x = _scrollArea.y = indent;
			_content.addChild(_scrollArea);
			var contentHeight:int = _content.height;
			
			_scrollArea.addControls(_itemsData.length); 
			
			if (_scrollArea.scrollCheck()) 
			{
				_scrollBar = new UIScrollBar();

				_scrollBar.x = width - _scrollBar.width * 0.5 - indent;
				_scrollBar.y = indent + _scrollBar.controlSize;
				_scrollBar.resize(contentHeight - (indent + _scrollBar.controlSize) * 2);
				
				_scrollBar.setSliderParams(0, _scrollArea.scrollAmount, 0);
				_scrollBar.lineSize = 5;
				_scrollBar.addEventListener(Event.CHANGE, scrollBarHandler);
				_content.addChild(_scrollBar);	
			}
		}
		
		private function scrollBarHandler(e:Event):void 
		{
			_scrollArea.scroll(_scrollBar.value);
		}
		
		private function onHeaderMouseDown(e:MouseEvent):void 
		{
			dispatchEvent(new PanelEvent(PanelEvent.SELECT, this));
			
			if (_scrollBar)
				_scrollBar.value = 100;
			
			if (_scrollArea) _scrollArea.setupItems(_itemsData);
		}
		
		public function forceSelect():void
		{
			onHeaderMouseDown(null);
		}
		
		public function destroy():void
		{
			_header.removeEventListener(MouseEvent.MOUSE_DOWN, onHeaderMouseDown);
			removeChild(_header);
			_header = null;
		
			if (_content)
			{
				if (_scrollArea)
				{
					_scrollArea.destroy();
					_content.removeChild(_scrollArea);
					_scrollArea = null;
				}
				
				if (_scrollBar)
				{
					_scrollBar.destroy();
					_scrollBar.removeEventListener(Event.CHANGE, scrollBarHandler);
					_content.removeChild(_scrollBar);
					_scrollBar = null;
				}
				removeChild(_content);
				_content = null;
			}
		}
		
		public function get enabled():Boolean 
		{
			return _enabled;
		}
		
		public function set enabled(value:Boolean):void 
		{
			_enabled = value;
			_header.mouseEnabled = value;
			_content.mouseChildren = !value;
		}
	}
}